package com.lists;

public class SimpleList extends Lista {

	@Override
	public void insertAtFront(Object insertItem) {
		if (isEmpty()) {
			firstNode = lastNode = new ListNode(insertItem);
		} else {
			firstNode = new ListNode(insertItem, firstNode);
		}
	}

	@Override
	public void insertAtLast(Object insertItem) {
		if (isEmpty()) {
			firstNode = lastNode = new ListNode(insertItem);
		} else {
			lastNode.setNextNode(new ListNode(insertItem));
			lastNode = lastNode.getNextNode();
		}
	}

	public void insertInOrder(Object insertItem) {
		if (firstNode == lastNode) {
			if ((Integer) insertItem > (Integer) firstNode.getData()) {
				insertAtFront(insertItem);
			} else {
				insertAtLast(insertItem);
			}
		} else {
			ListNode current = firstNode;
			ListNode prev = null;
			while (current != null
					&& (Integer) insertItem < (Integer) current.getData()) {
				prev = current;
				current = current.getNextNode();
			}
			if (prev == null) {
				insertAtFront(insertItem);
			} else {
				if (prev == lastNode) {
					insertAtLast(insertItem);
				} else {
					ListNode nuevo = new ListNode(insertItem);
					prev.setNextNode(nuevo);
					nuevo.setNextNode(current);
				}
			}
		}
	}

	@Override
	public Object removeFromFront() throws EmptyListException {
		// TODO Auto-generated method stub
		if (isEmpty()) {
			throw new EmptyListException(name);
		}

		Object removedItem = firstNode.getData();

		if (firstNode == lastNode) {
			firstNode = lastNode = null;

		} else {
			firstNode = firstNode.getNextNode();
		}

		return removedItem;

	}

	@Override
	public Object removeFromBack() throws EmptyListException {

		if (isEmpty()) {
			throw new EmptyListException(name);
		}

		Object removedItem = lastNode.getData();

		if (firstNode == lastNode) {
			firstNode = lastNode = null;

		} else {

			ListNode current = firstNode;
			while (current.getNextNode() != lastNode) {
				current = current.getNextNode();
			}

			lastNode = current;
			current.setNextNode(null);
		}

		return removedItem;
	}

	@Override
	public Object removeObject(Object elemento) throws EmptyListException {

		if (isEmpty()) {
			throw new EmptyListException();
		}

		while ((Integer) elemento == (Integer) firstNode.getData()) {
			removeFromFront();
		}

		ListNode current = firstNode;
		ListNode previous = current;

		while (current != null) {
			if ((Integer) current.getData() == (Integer) elemento) {
				previous.setNextNode(current.getNextNode());
				if (current.getNextNode() == null) {
					lastNode = previous;
				}
			} else {
				previous = current;
			}
			current = current.getNextNode();
		}

		return elemento;
	}

	public static void main(String args[]) {

		SimpleList lsl = new SimpleList();
		lsl.insertAtFront(30);
		lsl.insertAtFront(50);
		lsl.insertAtFront(50);
		lsl.insertAtLast(50);
		lsl.insertAtLast(20);
		lsl.insertAtLast(40);
		lsl.insertAtLast(40);
		lsl.insertAtLast(40);

		lsl.print();

		lsl.removeObject(40);
		System.out.println("\n\n");
		lsl.print();

		lsl.insertAtLast(10);
		lsl.insertAtLast(40);
		lsl.insertAtFront(40);
		System.out.println("\n\n");
		lsl.print();

		lsl.removeObject(40);
		lsl.insertAtLast(10);
		lsl.insertInOrder(35);
		lsl.removeObject(50);
		System.out.println("\n\n");
		lsl.print();

	}

}
